﻿package com.wandian.util
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.utils.Dictionary;

    public class CLZHotkeyManager extends Object
    {
        private var _focusKeyDict:Dictionary = null;
        private var _focusObj:Object = null;
        private var _globalKeyDict:Dictionary = null;
        private var _globalParamsDict:Dictionary = null;
        private var _stage:Stage = null;
        private static var _instance:CLZHotkeyManager = null;

        public function CLZHotkeyManager(param1:Single)
        {
            if (param1 == null)
            {
                throw new Error("Can\'t create instance , Single is Null!");
            }
            return;
        }

        public function init(stage:Stage) : void
        {
            if (this._stage != null)
            {
                throw new Error("This manager is initialized!");
            }
            this._stage = stage;
            this._stage.addEventListener(KeyboardEvent.KEY_UP, this.onKey);
            this._focusKeyDict = new Dictionary(true);
            this._globalKeyDict = new Dictionary(true);
            this._globalParamsDict = new Dictionary(true);
            return;
        }

        public function destroy() : void
        {
            this._stage.removeEventListener(KeyboardEvent.KEY_UP, this.onKey);
            this._focusKeyDict = null;
            this._globalKeyDict = null;
            this._globalParamsDict = null;
            this._stage = null;
            return;
        }

        public function setFocusObj(param1:Object = null) : void
        {
            this._focusObj = param1;
            return;
        }

        public function registerFocusKey(param1:Object, param2:int, param3:Function, param4:Array = null) : void
        {
            var _loc_5:* = this.getFocusKeyCodeMap(param2);
            _loc_5[param1] = [param3, param4];
            return;
        }

        public function unRegisterFocusKey(param1:Object, param2:int, param3:Function) : void
        {
            var _loc_4:* = this.getFocusKeyCodeMap(param2);
            delete _loc_4[param1];
            return;
        }

        public function registerGlobalKey(param1:int, param2:Function, param3:Array = null) : void
        {
            var _loc_4:* = this.getGlobalKeyCodeArr(param1);
            CLZUtils.addElement(_loc_4, param2);
            this._globalParamsDict[param2] = param3;
            return;
        }

        public function unRegisterGlobalKey(param1:int, param2:Function) : void
        {
            var _loc_3:* = this.getGlobalKeyCodeArr(param1);
            if (CLZUtils.removeElement(_loc_3, param2))
            {
                delete this._globalParamsDict[param2];
            }
            return;
        }

        private function getFocusKeyCodeMap(param1:int) : Dictionary
        {
            if (this._focusKeyDict[param1] is Dictionary)
            {
                return this._focusKeyDict[param1];
            }
            var _loc_2:* = new Dictionary(true);
            this._focusKeyDict[param1] = new Dictionary(true);
            return _loc_2;
        }

        private function getGlobalKeyCodeArr(param1:int) : Array
        {
            if (this._globalKeyDict[param1] is Array)
            {
                return this._globalKeyDict[param1];
            }
            var _loc_2:* = [];
            this._globalKeyDict[param1] = [];
            return _loc_2;
        }

        private function onKey(event:KeyboardEvent) : void
        {
            var _loc_2:Function = null;
            var _loc_3:* = this.getFocusKeyCodeMap(event.keyCode);
            var _loc_4:* = _loc_3[this._focusObj];
            if (_loc_4)
            {
                _loc_2 = _loc_4[0];
                _loc_2.apply(null, _loc_4[1]);
            }
            var _loc_5:* = this.getGlobalKeyCodeArr(event.keyCode);
            var _loc_6:int = 0;
            while (_loc_6 < _loc_5.length)
            {
                
                _loc_2 = _loc_5[_loc_6];
                _loc_2.apply(null, this._globalParamsDict[_loc_2]);
                _loc_6 = _loc_6 + 1;
            }
            return;
        }

        public static function get instance() : CLZHotkeyManager
        {
            if (_instance == null)
            {
                _instance = new CLZHotkeyManager(new Single());
            }
            return _instance;
        }

	
    }
}
class Single{
	
}
